#! /bin/bash

source manualrun_HA_JMS

####################################################################
# deleteQueue()
# Inputs Assumed:
#   QUEUE_NAME - Establish variable value before calling this fcn()
####################################################################
deleteQueue() {

  ssh admin@${PRIMARY} "imaserver delete Queue  DiscardMessages=True  Name=${QUEUE_NAME} " > delete_queue_msg.log 2>&1
  rc=$?
  delete_queue_msg=` cat delete_queue_msg.log `
  echo "delete_queue_msg=${delete_queue_msg}"
  if [ ${rc} -ne 0 ]; then
     if [[ ${delete_queue_msg} != *" is not found."  ]]; then
        export QUEUE_NAME=${QUEUE_NAME}
        pollQueueConnections
        ssh admin@${PRIMARY} "imaserver delete Queue  DiscardMessages=True  Name=${QUEUE_NAME} "
        ssh admin@${PRIMARY} "datetime get"
     fi
   fi
}

####################################################################
###--------------------------------------------------------------###
# MAIN
# PRIMARY - IMA Server variable is sourced manualrun_HA_JMS 
###--------------------------------------------------------------###

if [ $# -eq 0 ]; then
   echo "you goofed system, what is the IMA Server?"
   read PRIMARY
else
   PRIMARY=$1
fi

# Eventually make these input parameters $2 and $3
declare -i APPNUM_START		# index starts - Number of Pub/Sub clients to create
declare -i APPNUM_END		# index ends   - Number of Pub/Sub clients to create

if [ $# -le 1 ]; then
   echo "What's QUEUE number of the First Queue to DELETE on IMA Server ${PRIMARY}?"
   read APPNUM_START
else
   APPNUM_START=${2}
fi

if [ $# -le 2 ]; then
   echo "What's QUEUE number of the LAST Queue to DELETE on IMA Server ${PRIMARY}?"
   read APPNUM_END
else
   APPNUM_END=${3}
fi

client-name
set -x
if [ $# -eq 4 ]; then
   USERHOST=$4
fi
set +x

for id in $(eval echo {${APPNUM_START}..${APPNUM_END}} )
do
#  client-name
   USERCNT=`printf "%03d" ${id}`


   declare SUB_USERNAME=c${USERCNT}0${USERHOST}
   declare PUB_USERNAME=c${USERCNT}1${USERHOST}

   declare PUB_QUEUE_NAME=${QUEUE_APP}"/"${QUEUE_USER}${SUB_USERNAME}
   declare SUB_QUEUE_NAME=${QUEUE_USER}${SUB_USERNAME}"/"${QUEUE_APP}
# when not using MDB PUB/SUB Topics need to match -- COMMENT NEXT LINE WHEN USING MDB.
#  declare PUB_QUEUE_NAME=${SUB_QUEUE_NAME}

set -x
   QUEUE_NAME=${SUB_QUEUE_NAME}
   deleteQueue
   QUEUE_NAME=${PUB_QUEUE_NAME}
   deleteQueue
set +x

done


